import scrapy
from scrapy import Request
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from app import evn_

from spiders.items import SectorFundsItem
from db.dao import IndustryInfoDao
from utils.util import CalculationTool


class SectorQuotationSpider(scrapy.Spider):
    '''
    行业行情数据（涨幅，资金情况）
    '''
    name = 'sector_quotation'

    def start_requests(self):
        '''

        https://stock.eastmoney.com/hangye.html
        代替start_urls,从数据库读取板块编码，拼接读取板块的行情数据
        :return: 示例：http://quote.eastmoney.com/bk/90.BK0421.html
        '''
        url = 'http://quote.eastmoney.com/bk/90.%s.html'
        dao = IndustryInfoDao()
        find_all = dao.findAll()
        if find_all:
            for industryInfo in find_all:
                if industryInfo:
                    code = industryInfo.__getattribute__("code")
                    if code:
                        start_url = url % code
                        yield Request(start_url, callback=self.parse_item)

    def __init__(self, *args, **kwargs):
        chrome_options = Options()
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('lang=zh_CN.UTF-8')
        chrome_options.add_argument('headless')  # 无头浏览器
        chrome_options.add_argument('--no-sandbox')  # 必要！！
        chrome_options.add_argument('--disable-dev-shm-usage')  # 必要！！
        prefs = {
            "profile.managed_default_content_settings.images": 2,  # 禁止加载图片
            'permissions.default.stylesheet': 2,  # 禁止加载css
        }
        chrome_options.add_experimental_option("prefs", prefs)
        self.bro = webdriver.Chrome(executable_path=evn_.CHROMEDRIVER_PATH, chrome_options=chrome_options)

    def parse_item(self, response):
        # 涨幅情况
        increase_tbody = response.xpath('//*[@id="app"]/div/div/div[8]/div[3]/div[2]/table/tbody')

        item = SectorFundsItem()
        item['industry_name'] = response.xpath('//*[@id="app"]/div/div/div[6]/div[1]/span[1]/text()').extract_first()
        item['industry_code'] = response.xpath('//*[@id="app"]/div/div/div[6]/div[1]/span[2]/text()').extract_first()
        item['open'] = increase_tbody.xpath('tr[1]/td[2]/span/span/text()').extract_first()
        item['close'] = increase_tbody.xpath('tr[1]/td[1]/span/span/text()').extract_first()
        item['high'] = increase_tbody.xpath('tr[2]/td[2]/span/span/text()').extract_first()
        item['low'] = increase_tbody.xpath('tr[2]/td[2]/span/span/text()').extract_first()
        item['preclose'] = response.xpath(
            '//*[@id="app"]/div/div/div[7]/div[2]/div/ul/li[7]/span/span/text()').extract_first()
        item['pctChg'] = CalculationTool.cutOutTail(
            increase_tbody.xpath('tr[3]/td[1]/span/span/text()').extract_first())
        item['volume'] = CalculationTool.cutOutTail(
            increase_tbody.xpath('tr[4]/td[1]/span/span/text()').extract_first())
        item['amount'] = CalculationTool.cutOutTail(
            increase_tbody.xpath('tr[4]/td[2]/span/span/text()').extract_first())
        item['turn'] = CalculationTool.cutOutTail(increase_tbody.xpath('tr[5]/td[1]/span/span/text()').extract_first())
        item['peTTM'] = CalculationTool.cutOutTail(increase_tbody.xpath('tr[5]/td[2]/span/span/text()').extract_first())

        item['inner'] = CalculationTool.cutOutTail(increase_tbody.xpath('tr[6]/td[1]/span/span/text()').extract_first())
        item['outer'] = CalculationTool.cutOutTail(increase_tbody.xpath('tr[6]/td[2]/span/span/text()').extract_first())
        item['amplitude'] = CalculationTool.cutOutTail(
            increase_tbody.xpath('tr[7]/td[1]/span/span/text()').extract_first())
        item['rising_nums'] = increase_tbody.xpath('tr[7]/td[2]/span/span/text()').extract_first()

        item['decliner_nums'] = increase_tbody.xpath('tr[8]/td[1]/span/span/text()').extract_first()
        item['flat_nums'] = increase_tbody.xpath('tr[8]/td[2]/span/span/text()').extract_first()

        # 资金流入情况
        in_out_div = response.xpath('//*[@id="app"]/div/div/div[8]/div[3]/div[1]/div[2]/div/table[1]/tbody')
        item['main_inflow'] = CalculationTool.cutOutTail(
            in_out_div.xpath('tr[1]/td[2]/span/text()').extract_first())  # 主力流入
        item['main_outflow'] = CalculationTool.cutOutTail(
            in_out_div.xpath('tr[2]/td[2]/span/text()').extract_first())  # 主流流出
        item['main_net_inflow'] = CalculationTool.convert2Y(
            in_out_div.xpath('tr[3]/td[2]/span/text()').extract_first())  # '主力资金净流入',
        # leading_stock = scrapy.Field()  # '领涨股票',
        stock_time = response.xpath('//*[@id="app"]/div/div/div[6]/div[1]/span[3]/text()').extract_first()
        if (stock_time):
            item['create_time'] = stock_time.split(' ')[0][1:]
        yield item
